import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询备件销售订单'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备件销售订单详情'
});
const newOrderDesc = formatMessage({
    id: 'api.message.newOrderDesc',
    defaultMessage: '新增备件销售订单'
});
const modifyDesc = formatMessage({
    id: 'api.message.modifyDesc',
    defaultMessage: '修改备件销售订单'
});
const approveDesc = formatMessage({
    id: 'api.message.approveDesc',
    defaultMessage: '备件销售订单审核'
});
const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '备件销售订单作废'
});
const partQueryDesc = formatMessage({
    id: 'api.message.partQueryDesc',
    defaultMessage: '查询备件信息'
});
const getDealersDesc = formatMessage({
    id: 'api.message.getDealersDesc',
    defaultMessage: '查询总代信息'
});
const submitDesc = formatMessage({
    id: 'api.message.submitDesc',
    defaultMessage: '提交备件销售订单'
});
const partOrderPlanQueryDesc = formatMessage({
    id: 'api.message.partOrderPlanQueryDesc',
    defaultMessage: '查询需求计划单'
});
const partOrderPlanDetailDesc = formatMessage({
    id: 'api.message.partOrderPlanDetailDesc',
    defaultMessage: '查询需求计划详情'
});
const detailExportDesc = formatMessage({
    id: 'api.message.detailExportDesc',
    defaultMessage: '导出清单'
});
const importTempDesc = formatMessage({
    id: 'api.importTempDesc',
    defaultMessage: '下载导入清单模板'
});
const getListHisDesc = formatMessage({
    id: 'api.message.getListHisDesc',
    defaultMessage: '查询备件销售历史订单'
});
const getDetailHisDesc = formatMessage({
    id: 'api.message.getDetailHisDesc',
    defaultMessage: '查询备件销售历史订单详情'
});
const exportDetailsDesc = formatMessage({
    id: 'api.message.exportDetailsDesc',
    defaultMessage: '导出'
});

//初始化
export const getInitData = () => request(`${baseUrl}/ui/partSaleOrder/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getListDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partSaleOrders/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/partSaleOrders/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//新增
export const newOrder = data => request(`${baseUrl}/partSaleOrders`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));
//修改
export const modifyOrder = (id, data) => request(`${baseUrl}/partSaleOrders/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));
//提交
export const submitData = (id, data) => request(`${baseUrl}/partSaleOrders/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));
//审核通过
export const approveData = (id, data) => request(`${baseUrl}/partSaleOrders/${id}/pass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));
//审核驳回
export const rejectData = (id, data) => request(`${baseUrl}/partSaleOrders/${id}/reject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));
//作废
export const abandonData = (id, data) => request(`${baseUrl}/partSaleOrders/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));

//查询备件
export const partQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts/forPartSaleOrder?${queryString}`, null, dto.PartList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partQueryDesc));
};
//查询备件替代件
export const partReplacementQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts/forPartSaleOrder/query?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partQueryDesc));
};
// 快捷查询总代
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forPartSaleOrder/simple?${queryString}`, null, dto.DealerSimpleList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forPartSaleOrder?${queryString}`, null, dto.DealerList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
//查询备件
export const partOrderPlanQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partOrderPlans/forPartSaleOrder?${queryString}`, null, dto.PartOrderPlan)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partOrderPlanQueryDesc));
};
// 查询详情
export const partOrderPlanDetail = id =>
    request(`${baseUrl}/partOrderPlans/forPartSaleOrder/${id}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partOrderPlanDetailDesc));

// 导出清单
export const detailExport = data => request(`${baseUrl}/partSaleOrders/export/details`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, detailExportDesc)).then(res => res.data);

// 查询配件销售订单
export const getLastTime = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partSaleOrders/lastTime?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
export const partOrderPartImportTemp = () => request(`${baseUrl}/parts/forPartSaleOrderImport/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);
// 查询历史订单列表
export const getListHis = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partSaleOrderHises/forCurrentUser?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListHisDesc));
};
// 查询历史详情
export const getDetailHis = id =>
    request(`${baseUrl}/partSaleOrderHises/${id}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailHisDesc));
// 导出清单
export const exportDetails = id => request(`${baseUrl}/partSaleOrders/${id}/export`, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDetailsDesc))
    .then(res => res.data);
